<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title></title>
		<!-- hiprint -->
		<link href="${ctxPath}/assets/hiprint/css/hiprint.css" rel="stylesheet">
		<link href="${ctxPath}/assets/hiprint/css/print-lock.css" rel="stylesheet">

		<script src='${ctxPath}/assets/mui/js/jquery-1.11.3.js'></script>
		<script src='${ctxPath}/assets/hiprint/polyfill.min.js'></script>
		<script src='${ctxPath}/assets/hiprint/hiprint.bundle.js'></script>
		<script src='${ctxPath}/assets/hiprint/socket.io.js'></script>
		<script src='${ctxPath}/assets/hiprint/plugins/jquery.hiwprint.js'></script>
		<script src='${ctxPath}/assets/hiprint/printData/autoPrintByDispatchNumber-json.js'></script>
	</head>
	<style>
		body{
			text-align: center;
			padding-top: 200px;
		}
		.content{

		}
	</style>
	<body>
		<h2>自助打印发车单，请输入运输单号</h2>
		<div class="content">
			<input type="text" name="dispatchNumber" id="dispatchNumber" style="height: 30px; width: 400px;">
		</div>

		<script type="text/jscript">
            var hiprintTemplate;
            $(document).ready(function () {
            	//初始化打印插件
				hiprint.init();

				$('#dispatchNumber').focus();

				// 为该元素添加失去焦点事件的事件监听器
				document.getElementById('dispatchNumber').addEventListener('blur', function() {
					$('#dispatchNumber').focus();
				});

				var nowDispatchNumber = "";
				document.addEventListener('keydown', function(event) {
					const key = event.key || event.keyCode;
					if (key === 'Enter' || key === 'LineFeed') {
						var dispatchNumber = $('#dispatchNumber').val();
						if(dispatchNumber == ''){
							alert("请先输入运输单号！");
							return false;
						}
						//传到后台获取打印信息
						if(nowDispatchNumber == dispatchNumber){
							alert(nowDispatchNumber+"已打印，请输入其他运输单号！");
							return false;
						}else{
							nowDispatchNumber = dispatchNumber;
							//打印处理
							printHtml(nowDispatchNumber);
						}
						$('#dispatchNumber').val('');
					}
				});

				function printHtml(dispatchNumber) {
					$.ajax({
						url:'${ctxPath}/rest/transport/getAutoPrint',
					    type:'post',
					    dataType:'json',
					    data:{"dispatchNumber":dispatchNumber},
					    success:function(data){
					 		if(data.code == 200 && data.data.length > 0){
								hiprintTemplate = new hiprint.PrintTemplate({ template: autoPrintByDispatchNumberJson });
								hiprintTemplate.print(data.data, {});
					 		}
					    },
					    error: function(data){
							alert("查询发货单异常!");
					    }
					});
				};

            });
		</script>
	</body>
</html>